package cn.tedu.web.mapper;

import cn.tedu.web.entity.Goods;
import cn.tedu.web.entity.GoodsAddition;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 商品表 Mapper 接口
 * </p>
 *
 * @author issen
 * @since 2021-09-24
 */
@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
    /**
     * 新增一个新的商品
     */
    public int insert(Goods goods);

    /**
     * 批量插入商品的附加信息
     */
    public Integer addGoodsAddition(@Param("adds") List<GoodsAddition> additions);

    /**
     * 分页获取管理员的列表数据
     * 
     * @param offset 偏移量
     * @param limit  限制量
     * @return
     */
    @Select("SELECT `id`, `cid`, `name`, `price`, `status`, `image`, `introduction` FROM ec_goods LIMIT #{o}, #{l}")
    public List<Goods> findGoods(@Param("o") int offset, @Param("l") int limit);

    /**
     * 求商品的总数
     */
    @Select("SELECT COUNT(1) FROM ec_goods")
    public Integer count();

    /**
     * 根据 CID 查询商品的总数
     */
    @Select("SELECT COUNT(1) FROM ec_goods WHERE cid = #{cid}")
    public Integer countGoodsByCid(@Param("cid") Integer id);

    /**
	 * 查询上新产品：新产品指的是在三个月内上架的六个最新商品
	 */
	@Select("SELECT `id`, `cid`, `name`, `price`, `image` FROM ec_goods WHERE `status`= 1 AND TIMESTAMPDIFF(MONTH, `on_time`, NOW()) <= 3 ORDER BY `on_time` DESC LIMIT 6")
	public List<Goods> recentlyGoods();
}
